/**
 *    Copyright (C) 2009 Victor Smirnov <aist11@gmail.com>
 *
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License along
 *    with this program; if not, write to the Free Software Foundation, Inc.,
 *    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 *    See LICENSE.exceptions in the root folder of this project for possible
 *    exceptions
 *
 */

#ifndef _CUBEFS_CORE_API_METADATA_GROUP_HPP
#define _CUBEFS_CORE_API_METADATA_GROUP_HPP

#include <cubefs/api/metadata/group.hpp>
#include <cubefs/core/api/metadata/metadata.hpp>

#define CUBEFS_SOURCE "cubefs/core/api/metadata/group.hpp:" CUBEFS_AT

namespace cubefs { namespace api {

template <typename Interface>
class MetadataGroupImplT: public MetadataImplT<Interface> {
	typedef MetadataGroupImplT<Interface> 		Me;
	typedef MetadataImplT<Interface> 			Base;
public:

	MetadataGroupImplT(StringRef name, const MetadataList &content): Base(name, Metadata::GROUP), content_(content.size()) {
		for (Int c = 0; c < content.size(); c++) {
			content_[c] = content[c];
		}
	}

	virtual ~MetadataGroupImplT() throw ()
	{
		for (Int c = 0; c < content_.size(); c++) {
			if (content_[c]->GetTypeCode() != Metadata::PAGE &&
				content_[c]->GetTypeCode() != Metadata::MODEL &&
				content_[c]->GetTypeCode() != Metadata::CONTAINER)
			{
				delete content_[c];
			}
		}
	}

	virtual Int Size() const {
		return content_.size();
	}

	virtual Metadata* GetItem(Int idx) const {
		return content_[idx];
	}

	static void Init() {
	    AbstractType *type = new AbstractType(TypeNameFactory<Interface>::name(), Metadata::MyType());
	    type_ = type;

	    type->AddOperation(new BasicOperation<Typed* (Me::*)(Int), Metadata* (Me::*)(Int) const>(type, Metadata::MyType(), "GetItem", "idx", &Me::GetItem));
	    type->AddOperation(new BasicOperation<Int (Me::*)(), Int (Me::*)() const>(type, "Size", &Me::Size));
	}

    CUBEFS_DECLARE_REFLECTION;

private:
    MetadataList content_;
};

template <typename Interface>
Type* MetadataGroupImplT<Interface>::type_ 					= NULL;

typedef MetadataGroupImplT<MetadataGroup> 					MetadataGroupImpl;



}}

#undef CUBEFS_SOURCE
#endif
